<?php

/**
 * This is the model class for table "user_event_relation".
 *
 * The followings are the available columns in table 'user_event_relation':
 * @property string $event_id
 * @property string $member_id
 * @property string $type
 * @property string $status
 */
class UserEventRelation extends CActiveRecord
{
	const TYPE_ADMIN=1;
	const TYPE_PARTICIPANT=2;
	const TYPE_AUDIENCE=3;

	/**
	 * Returns the static model of the specified AR class.
	 * @return UserEventRelation the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user_event_relation';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('event_id, member_id', 'required'),
			array('event_id, member_id', 'length', 'max'=>10),
			array('type', 'length', 'max'=>11),
			array('status', 'length', 'max'=>7),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('event_id, member_id, type, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * This method will return if the user has joined the cetrain event specified by $eventid.
	 */
	public static function isUserJoinedEvent($eventid){
		if(Yii::app()->user->isGuest){
			return false;
		}

		$uid= Yii::app()->user->id;
		$conditions='event_id=:event_id AND member_id=:member_id';
		$params=array(':event_id'=>$eventid, ':member_id'=>$uid);

		if(self::model()->exists($conditions,$params)){
			return true;
		}
		return false;
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'event' => array(self::BELONGS_TO, 'Event', 'event_id'),
			'member' => array(self::BELONGS_TO, 'Member', 'member_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'event_id' => 'Event',
			'member_id' => 'Member',
			'type' => 'Type',
			'status' => 'Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('event_id',$this->event_id,true);
		$criteria->compare('member_id',$this->member_id,true);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('status',$this->status,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}
